package com.dnjn.happiness.survey.controller.common;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.common.cloud.controller.BaseController;
import com.dnjn.happiness.survey.domain.CheckAnswer;
import com.dnjn.happiness.survey.domain.vo.CheckAnswerVO;
import com.dnjn.happiness.survey.service.ICheckAnswerService;
import com.dnjn.happiness.common.log.annotation.Log;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.log.enums.BusinessType;
import com.dnjn.happiness.common.core.utils.bean.BeanUtils;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Arrays;
import java.util.List;


/**
 * 考核答案Controller
 *
 * @author happiness
 * @date 2024-04-03
 */

@Slf4j
public class CommonCheckAnswerController extends BaseController {
    @Autowired
    protected ICheckAnswerService checkAnswerService;

    /**
     * 查询考核答案列表
     */
    @Log(title = "考核答案", businessType = BusinessType.LIST)
//    @RequiresPermissions("@ss.hasAnyPermi('check:answer:list,ignore:answer:list')")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody CheckAnswerVO vo, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        CheckAnswer checkAnswer=new CheckAnswer();
        BeanUtils.copyPropsIgnoreNull(vo,checkAnswer);
        Page page = Page.of(current, size);
        Page<CheckAnswer> list = checkAnswerService.page(page, Wrappers.query(checkAnswer));
        return success(list);
    }

    /**
     * 获取考核答案详细信息
     */
    @Log(title = "考核答案", businessType = BusinessType.SELECT)
//    @RequiresPermissions("@ss.hasAnyPermi('check:answer:query,ignore:answer:query')")
    @GetMapping(value = "/{answerId}")
    public AjaxResult get(@PathVariable("answerId") Long answerId) {
        return success(checkAnswerService.getById(answerId));
    }

    /**
     * 新增考核答案
     */
    @Log(title = "考核答案", businessType = BusinessType.INSERT)
//    @RequiresPermissions("@ss.hasAnyPermi('check:answer:add,ignore:answer:add')")
    @PostMapping("/add")
    @Operation(summary = "add添加答案")
    public AjaxResult add(@RequestBody CheckAnswerVO vo) {

        checkAnswerService.saveList(vo);

        return success();
    }

    /**
     * 修改考核答案
     */
    @Log(title = "考核答案", businessType = BusinessType.UPDATE)
//    @RequiresPermissions("@ss.hasAnyPermi('check:answer:edit,ignore:answer:edit')")
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody CheckAnswerVO vo) {
        CheckAnswer checkAnswer=new CheckAnswer();
        BeanUtils.copyPropsIgnoreNull(vo,checkAnswer);
        return toAjax(checkAnswerService.updateById(checkAnswer));
    }

    /**
     * 删除考核答案
     */
    @Log(title = "考核答案", businessType = BusinessType.DELETE)
//    @RequiresPermissions("@ss.hasAnyPermi('check:answer:remove,ignore:answer:remove')")
    @DeleteMapping("/{answerIds}")
    public AjaxResult remove(@PathVariable Long[] answerIds) {
        return toAjax(checkAnswerService.removeBatchByIds(Arrays.asList(answerIds)));
    }



    @Log(title = "考核答案", businessType = BusinessType.SELECT)
//    @RequiresPermissions("@ss.hasAnyPermi('check:answer:query,ignore:answer:query')")
    @GetMapping(value = "/getAnswerByCheckItemId")
    public AjaxResult getAnswerByCheckItemId( Long checkItemId) {
        List<CheckAnswer> answerList=checkAnswerService.getAnswerByCheckItemId(checkItemId);
        return success(answerList);
    }

    @Log(title = "考核答案", businessType = BusinessType.SELECT)
//    @RequiresPermissions("@ss.hasAnyPermi('check:answer:query,ignore:answer:query')")
    @GetMapping(value = "/getAnswerByCheckItemIdAndUserId")
    public AjaxResult getAnswerByCheckItemIdAndUserId( Long checkItemId,Long superiorUserId,Long subordinateUserId) {
        return checkAnswerService.getAnswerByCheckItemIdAndUserId(checkItemId,superiorUserId,subordinateUserId);
    }
}
